package com.caird.pmp.iot.handler.impl;

import com.caird.pmp.iot.entity.DeviceShadow;
import com.caird.pmp.iot.handler.HandlerBase;
import com.caird.pmp.iot.message.IMessage;
import com.caird.pmp.iot.message.impl.DeviceMessage01E1;
import com.caird.pmp.iot.message.impl.ServerMessage8001;
import com.caird.pmp.iot.server.BbChannelManager;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

import java.util.Date;

@Slf4j
@Controller
public class Handler01E1 extends HandlerBase {

    @Override
    public boolean handle(Channel channel, IMessage message) {
        DeviceMessage01E1 dm = (DeviceMessage01E1)message;

        ServerMessage8001 sm = ServerMessage8001.success(dm, BbChannelManager.getFlowNo(channel));

        writeAndFlush(channel, sm.encode());

        //保存日志信息
        DeviceShadow shadow = getShadow(dm.getSimNo());
        shadow.setLogTime(new Date());
        shadow.setLog(dm.getLog());
        shadow.setLogCount(null == shadow.getLogCount() ? 1 : shadow.getLogCount() + 1);
        setShadow(dm.getSimNo(), shadow);

        return true;
    }
}
